import cloud from '@lafjs/cloud'
const db = cloud.mongo.db;

export default async function (ctx: FunctionContext) {
  console.log('接收到的数据', ctx.body)
  let { data, dbName } = ctx.body
  await db.collection(dbName).deleteMany({})
  //return cloud.utils.requestReturn({ data: "" })
  for (let index = 0; index < data.length; index++) {
    const element = data[index];
    let res = await cloud.nw.db.findByWhereJson({
      dbName,
      whereJson: { _id: element._id },
    });
    if (res && res._id) {
      let dataJson = { ...element }
      delete dataJson._id
      await cloud.nw.db.update({
        dbName,
        whereJson: { _id: element._id },
        dataJson
      });
    } else {
      await db.collection(dbName).insertOne(element);
    }
  }
  return cloud.utils.requestReturn({ data: "" })
}
